import { DNYMIC_LIKE_STATUS, FOLLOW_STATUS, REPLY_TYPE } from '@/enum/dynamic.enum';
import { GET, POST } from '@/libs';
import { UserInfo } from '@/store/modules/user.module';

export type DynamicContent = {
  type: number;
  content: string;
  images: string;
};

export type Dynamic = {
  id: number;
  userInfo: UserInfo | null;
  userId: number;
  createTime: string;
  updateTime: string;
  views: number;
  love: number;
  loveStatus: DNYMIC_LIKE_STATUS;
  comment: number;
  followStatus: FOLLOW_STATUS;
  imageParams?: {
    width?: number;
    height?: number;
  };
} & DynamicContent;

type PageResponse = {
  current: number;
  size: number;
  total: number;
  list: Array<Dynamic>;
};

export const getDyamicList = (current = 1, size = 10, lastId = 0, type = 0) => {
  return GET<PageResponse>({
    url: '/dynamic/getDynamicList',
    params: {
      current,
      size,
      lastId,
      type
    }
  });
};

export const createDynamic = (data: DynamicContent) => {
  return POST({
    url: '/dynamic/create',
    data
  });
};

export const getDynamicDetail = (id: number) => {
  return GET<Dynamic>({
    url: '/dynamic/getDynamicDetail',
    params: {
      id
    }
  });
};

export type ReplyResposeMessage = {
  createTime: string;
  updateTime: string;
  id: number;
  type: REPLY_TYPE;
  replyUserId: number;
  replyTargetId: number;
  replyTargetUserId: number;
  dynamicId: number;
  commentId: number;
  content: string;
  userInfo: UserInfo;
  replyTargetUserInfo: UserInfo;
};

export type CommentResposeMessage = {
  createTime: string;
  updateTime: string;
  id: number;
  commentUserId: number;
  commentDynamicId: number;
  commentContent: string;
  userInfo: UserInfo;
  reply: ReplyResposeMessage[];
};

export const getDynamicComment = (dynamicId: number) => {
  return GET<CommentResposeMessage[]>({
    url: '/comment/getDynamicComment',
    params: {
      dynamicId
    }
  });
};

export type CommentMessage = {
  commentDynamicId: number;
  commentContent: string;
  content?: string;
};

export type ReplyMeesage = {
  type: REPLY_TYPE;
  replyTargetId: number;
  replyTargetUserId: number;
  dynamicId: number;
  content: string;
  commentId: number;
};

export const sendComment = (data: CommentMessage) => {
  return POST<CommentResposeMessage>({
    url: '/comment/createComment',
    data
  });
};

export const sendReply = (data: ReplyMeesage) => {
  return POST<ReplyResposeMessage>({
    url: '/comment/createReply',
    data
  });
};

type LikeResponse = {
  createTime: string;
  updateTime: string;
  id: number;
  likeType: number;
  likeUserId: number;
  likeTargetId: number;
  status: DNYMIC_LIKE_STATUS;
};

export const toLove = (dynamicId: number) => {
  return POST<LikeResponse>({
    url: '/dynamic/toLove',
    data: {
      dynamicId
    }
  });
};

export const getPersonalDynamicList = (userId: number, current = 1, size = 10, lastId = 0) => {
  return GET<PageResponse>({
    url: '/dynamic/getPersonalDynamicList',
    params: {
      current,
      size,
      lastId,
      userId
    }
  });
};
